<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="author" content="wangbo">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
    </div>

    <script type="text/javascript" src="../js/react.development.js">    </script>
    <script type="text/javascript" src="../js/react-dom.development.js">    </script>
    <script type="text/javascript" src="../js/prop-types.js">    </script>
    <script type="text/javascript" src="../js/babel.min.js">    </script>
    <script type="text/babel">
        class Button extends React.Component {
            constructor(props) {
                super(props);
                this.state = { data: 0 };
                this.setNewNumber = this.setNewNumber.bind(this);
            }

            setNewNumber() {
                this.setState({ data: this.state.data + 1 })
            }
            
            render() {
                return (
                    <div>
                        <button onClick={this.setNewNumber}>声明周期 {this.state.data}</button>
                        <Content myNumber={this.state.data}></Content>
                    </div>
                );
            }
        }
        class Content extends React.Component {
            // 在组件收到新 props或 state但还没有 render时调用。未在初始化时调用。组件更新完成后。
            // this.setState()会触发componentWillUpdate(),如果我们componentWillUpdate()中触发状态更改,
            // 我们将以无限循环结束.
            // this.setState() 保证它不会在每次渲染的时候执行，
            componentWillUpdate(nextProps, nextState) {
                console.log(nextProps)
                console.log('Component 将更新');
            }
            render() {
                return (
                    <div>
                        <h3>{this.props.myNumber}</h3>
                    </div>
                );
            }
        }
        ReactDOM.render(
            <div>
                <Button />
            </div>,
            document.getElementById('app')
        );
    </script>
</body>

</html>